<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">

            <h:form id="proprietarioForm" enctype="multipart/form-data">
                <br/>
                <h2 class="title">Pesquisar Contribuinte</h2>
                <p:growl id="messages" showDetail="false" autoUpdate="true"  /> 
                <p:panel id="personPanel" styleClass="container_24 clearfix both ">
                    <h4 class="title">Dados Gerais</h4>

                    <div class="grid_24" style="margin-bottom: -5px;">
                        <p:outputLabel id="personOutputLabel" value="Pessoa: " styleClass="grid_3" style="top: 6px" />
                        <p:selectOneRadio id="personRadioButton" 
                                          value="#{managerProprietario.pesquisarProprietario.tipoDocumento}" >
                            <f:selectItems value="#{managerProprietario.tipoDocumentoList()}" />
                            <p:ajax event="change" update=":proprietarioForm:proprietarioPanel" />
                        </p:selectOneRadio>
                    </div>

                    <div class="clear"/>
                    <p:panel id="proprietarioPanel" 
                             style="margin-left: -7px; margin-top: -20px;" 
                             styleClass="container_24 clearfix both " >

                        <div class="clear"/>

                        <p:outputLabel id="tipoDocumentoLabel" 
                                       value="#{managerProprietario.pesquisarProprietario.tipoDocumento}: " 
                                       styleClass="grid_3"/>
                        <f:validateBean disabled="true" >
                            <p:inputMask id="tipoDocumentoInput" 
                                         value="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CPF' ? 
                                                  managerProprietario.pesquisarProprietario.cpf : managerProprietario.pesquisarProprietario.cnpj}"
                                         mask="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CPF' ? 
                                                 '999.999.999-99':'99.999.999/9999-99'}"
                                         styleClass="grid_8" > 
                            </p:inputMask>
                        </f:validateBean>

                        <p:outputLabel value="Estado Civil" styleClass="grid_4 alpha" id="estadoCivilOutputlabel"
                                       rendered="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CPF'}" 
                                       for="estadoCivil" />
                        <div class="grid_8" style="margin-top: -4px">
                            <p:selectOneMenu id="estadoCivil" panelStyle="width:150px;"
                                             rendered="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CPF'}"
                                             value="#{managerProprietario.pesquisarProprietario.estadoCivil}" >
                                <f:selectItem itemLabel="" itemValue="" />
                                <f:selectItems value="#{managerUtilitario.estadoCivil}"/>
                            </p:selectOneMenu> 
                        </div>

                        <div class="clear"/>
                        <p:outputLabel value="Nome" styleClass="grid_3" for="nomeInputText" 
                                       rendered="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CPF'}"/>
                        <p:inputText id="nomeInputText" styleClass="grid_20" 
                                     value="#{managerProprietario.pesquisarProprietario.nome}" 
                                     rendered="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CPF'}" />

                        <div class="clear"/>
                        <p:outputLabel value="Nome Fantasia" styleClass="grid_3 nomeFantasiaContribuinte" 
                                       for="razaoSocialInputText" style="margin-left: 14px !important" 
                                       rendered="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CNPJ'}" />
                        <p:inputText id="razaoSocialInputText" styleClass="grid_20"
                                     value="#{managerProprietario.pesquisarProprietario.nomeFantasia}" 
                                     rendered="#{managerProprietario.pesquisarProprietario.tipoDocumento.tipo eq 'CNPJ'}" />

                    </p:panel>

                </p:panel>

                <br/>
                <div class="buttonAction">
                    <p:commandButton value="Pesquisar" icon="ui-icon-search"
                                     update="@form, resultTable" id="pesquisarCommandButton"
                                     styleClass="ui-priority-primary"
                                     actionListener="#{managerProprietario.pesquisar()}" />

                    <p:commandButton value="Limpar" icon="ui-icon-clean"
                                     update="@form"
                                     actionListener="#{managerProprietario.limpar()}" />
                </div >
                <br />

                <p:blockUI id="clockUi" block="resultPanel" trigger="pesquisarCommandButton, resultTable" > 
                    <p:graphicImage value="media/images/ajax-loader.gif"/>
                </p:blockUI>

                <h4 class="title">Resultado</h4>
                <p:panel id="resultPanel" style="background: none; border: none;" 
                         styleClass="container_24 clearfix both ">

                    <p:dataTable id="resultTable" var="item" value="#{managerProprietario.dataModel}"
                                 styleClass="grid_24" rowIndexVar="rowid"
                                 binding="#{managerProprietario.searchResults}"
                                 rows="5" paginator="true" lazy="true"
                                 paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                                 currentPageReportTemplate="{currentPage} de {totalPages}"
                                 paginatorPosition="bottom" emptyMessage="Nenhum registro encontrado">

                        <f:facet name="header">
                            <p:outputLabel value="Proprietários #{managerProprietario.searchResults.rowCount eq 0 ? '' : '('.concat(managerProprietario.searchResults.rowCount).concat(')')}"/>
                        </f:facet> 
                        <p:column headerText="Ação" style="width: 50px; vertical-align: middle;">  
                            <p:button icon="ui-icon-pencil" href="editarproprietario.xhtml?eProprietario=#{item.id}" 
                                      style="width: 25px;" title="Editar"/>
                            <p:button icon="ui-icon-search" href="visualizarproprietario.xhtml?vProprietario=#{item.id}" 
                                      style="width: 25px;" title="Visualizar"/>
                            <p:commandButton  style="width: 20px; height: 20px;" title="Excluir"
                                              oncomplete="dlg.show()"
                                              icon="ui-icon-trash">
                                <f:setPropertyActionListener target="#{managerProprietario.deleteProprietario}" value="#{item}" />
                            </p:commandButton>

                        </p:column> 
                        <p:column headerText="Nome / Fantasia" >  
                            <h:outputLabel value="#{item.tipoDocumento eq null or item.tipoDocumento.tipo eq 'CPF' ? item.nome : item.nomeFantasia}" />  
                        </p:column>
                        <p:column headerText="CPF / CNPJ">
                            <h:outputLabel value="#{item.tipoDocumento eq null or item.tipoDocumento.tipo eq 'CPF' ? 
                                                    managerUtilitario.mascaraCPF(item.cpf) : managerUtilitario.mascaraCNPJ(item.cnpj)}" />
                        </p:column>

                    </p:dataTable>

                </p:panel>

                <p:dialog id="modalDialog" header="Confirmação de remoção" widgetVar="dlg" modal="true" 
                          height="80" closable="false" resizable="false" >
                    <h:outputText value="Tem certeza que deseja remover este proprietario ?" />
                    <div class="buttonAction">
                        <p:commandButton value="Sim" update="@form" icon="ui-icon-check"
                                         actionListener="#{managerProprietario.remover()}" />
                        <p:commandButton value="Não" icon="ui-icon-close"
                                         oncomplete="dlg.hide()" />
                    </div>
                </p:dialog>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
